import { ref, toRefs } from 'vue'
import { useDictStore } from '@/stores/dict'
import { dictitemFindAll } from '@/api/system/dictitem'

/**
 * 获取字典
 * @param {String} args 字典名 可多参数
 * @returns {Object}
 */
export function useDict(...args) {
  const dictRes = ref({})
  return (() => {
    args.forEach((item) => {
      dictRes.value[item] = []
      const dicts = useDictStore().getDict(item)
      if (dicts) {
        dictRes.value[item] = dicts
      } else {
        dictitemFindAll({ belong: item, pageSize: -1 }).then((res) => {
          dictRes.value[item] = res.data
          useDictStore().setDict(item, dictRes.value[item])
        })
      }
    })
    return toRefs(dictRes.value)
  })()
}
